<template>
  <BaseDialog
    ref="baseDialog"
    :title="isEdit ? '编辑用户' : '新增用户'"
    @confrim="confrim"
    @cancel="close"
  >
    <BaseForm
      labelWidth="150px"
      :fromIems="fromIems"
      :size="'medium'"
      ref="BaseForm"
      @submit="submit"
      @edit="edit"
    ></BaseForm>
  </BaseDialog>
</template>

<script>
import { Message } from "element-ui";
import { list } from "@/api/role";
import { list as orgList } from "@/api/org";
import { getId } from "@/api/common";
import { add, edit } from "@/api/user";
import { rsaPassword, uploadImg, searchFormParams } from "@/utils";
export default {
  name: "add",
  data() {
    return {
      title: "",
      fromIems: [
        {
          valueType: "img",
          label: "用户头像",
          dataIndex: "headUrl",
          uplaod: (e) => this.handleUpload(e),
          rules: [{ required: true, message: "请上传", trigger: "blur" }],
          defaultValue: "",
        },

        {
          valueType: "input",
          label: "用户账号",
          dataIndex: "acc",
          rules: [{ required: true, message: "请输入", trigger: "blur" }],
        },
        {
          valueType: "input",
          label: "用户昵称",
          dataIndex: "nick",
          rules: [{ required: true, message: "请输入", trigger: "blur" }],
        },
        {
          valueType: "input",
          label: "用户手机",
          dataIndex: "mobile",
        },
        {
          valueType: "input",
          label: "用户密码",
          dataIndex: "password",
          type: "password",
          rules: [{ required: true, message: "请输入", trigger: "blur" }],
        },
        {
          valueType: "select",
          label: "所属机构",
          dataIndex: "orgCode",
          search: true,
          searchFn: (query) => this.handleSearchOrg(query),
          options: [],
          rules: [{ required: true, message: "请选择", trigger: "blur" }],
        },
        {
          valueType: "select",
          label: "用户角色",
          dataIndex: "roleId",
          options: [],
          rules: [{ required: true, message: "请选择", trigger: "blur" }],
        },
        {
          valueType: "input",
          label: "备注说明",
          dataIndex: "note",
          type:'textarea'
        },
      ],
      isEdit: false,
    };
  },
  methods: {
    open(row) {
      row ? (this.isEdit = true) : (this.isEdit = false);
      this.$refs.baseDialog.open();

      this.initRoleAndOrgList(); //查询角色和机构

      this.$nextTick(() => {
        if (row) {
          for (var k in row) {
            this.$set(this.$refs.BaseForm.form, k, row[k]);
            searchFormParams(this,'headUrl').defaultValue = row['headUrl']
          }
        } else {
          this.$refs.BaseForm.form = {};
        }
      });
    },
    async initRoleAndOrgList() {
      const res = await list({ pageIndex: 1, pageSize: 50 });
      if (!res.status && res.content) {
        searchFormParams(this, "roleId").options = res.content.map((el) => {
          return {
            label: el.name,
            value: el.roleId,
          };
        });
      }
      const result = await orgList({ pageIndex: 1, pageSize: 50 });
      if (!result.status && res.content) {
        searchFormParams(this, "orgCode").options = result.content.map((el) => {
          return {
            label: el.name,
            value: el.orgCode,
          };
        });
      }
    },
    // 搜索机构
    async handleSearchOrg(query) {
      console.log(query);
      const res = await orgList({ name: query, pageIndex: 1, pageSize: 50 });
      if (!res.status && res.content) {
        searchFormParams(this, "orgCode").options = res.content.map((el) => {
          return {
            label: el.name,
            value: el.orgCode,
          };
        });
      }
    },
    handleUpload(e) {
      uploadImg(e, 1024 * 1024, {}, 'head',(res) => {
        searchFormParams(this,'headUrl').defaultValue = res
      });
    },
    close() {
      searchFormParams(this,'headUrl').defaultValue = null
      this.$refs.baseDialog.close();
      this.$refs.BaseForm.rest();
    },
    confrim() {
      if (this.isEdit) {
        this.$refs.BaseForm.edit();
      } else {
        this.$refs.BaseForm.submit();
      }
    },
    async submit(data) {
      const result = await getId({ type: "user" });
      let params = { ...data };
      params.userId = result.content.id;
      params.password = rsaPassword(params.password);
      params.platform = 1; //平台
      const res = await add(params);
      if (!res.status) {
        this.$emit("success");
        Message.success("success");
        this.close();
      }
    },
    async edit(data) {
      const res = await edit({
        userId: data.userId,
        nick: data.nick,
        account: data.account,
        password: data.password,
        orgCode: data.orgCode,
        headUrl: data.headUrl,
        platform: 1,
        roleId: data.roleId,
      });
      if (!res.status) {
        this.$emit("success");
        Message.success("success");
        this.close();
      }
    },
  },
};
</script>

<style lang="scss" scoped></style>
